import time

import yaml
from selenium import webdriver

from utils.utility_method import driver_path
from selenium.webdriver.chrome.service import Service as ChromeService


def get_cookies():
    driver = webdriver.Chrome(service=ChromeService(driver_path))
    driver.get("https://oms.8zyun.cn/login?redirect=%2Fagentssy%2Findex")
    # 2. 等待20s，人工扫码操作
    time.sleep(20)
    # 3. 等成功登陆之后，再去获取cookie信息
    cookie = driver.get_cookies()
    # 4. 将cookie存入一个可持久存储的地方，文件
    # 打开文件的时候添加写入权限
    with open("../data/cookie.yaml", "w") as f:
        # 第一个参数是要写入的数据
        yaml.safe_dump(cookie, f)


def use_cookie():
    driver = webdriver.Chrome(service=ChromeService(driver_path))
    # 1. 访问企业微信主页面
    driver.get("https://oms.8zyun.cn/login?redirect=%2Fagentssy%2Findex")
    # 2. 定义cookie，cookie信息从已经写入的cookie文件中获取
    cookie = yaml.safe_load(open("../data/cookie.yaml"))
    # 3. 植入cookie
    for c in cookie:
        driver.add_cookie(c)

    driver.refresh()



    time.sleep(10)



if __name__ == '__main__':
    get_cookies()
    use_cookie()
